//
//  FeedbackViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2019/3/1.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "FeedbackViewController.h"

#import "UserManager.h"

@interface FeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong)   UILabel *placeLabel;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UILabel *topInfoLabel;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UITextField *wechatTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@end

@implementation FeedbackViewController

#pragma mark - VC Life

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = kLocalizedString(@"意见反馈", nil);
    
    [self initUI];
}

- (void)initUI {
    
    _placeLabel = [[UILabel alloc] init];
    _placeLabel.frame = CGRectMake(16,17,SCREEN_WIDTH - 62,[NSString sizeWithString:kLocalizedString(@"请输入问题描述",nil) font:[UIFont boldSystemFontOfSize:16] width:SCREEN_WIDTH - 62 height:0]);
    _placeLabel.lineBreakMode = NSLineBreakByCharWrapping;
    _placeLabel.numberOfLines = 0;
    _placeLabel.font = [UIFont boldSystemFontOfSize:16];
    _placeLabel.text = kLocalizedString(@"请输入问题描述",nil);
    _placeLabel.textColor = UIColorFromRGB(0xc9c9c9);
    [self.topView addSubview:_placeLabel];
    
    self.topInfoLabel.text = kLocalizedString(@"请留言 ，我们将尽快联系您并解决问题",nil);
    self.phoneTextField.placeholder = kLocalizedString(@"请输入您的手机号",nil);
    self.wechatTextField.placeholder = kLocalizedString(@"请输入您的微信号",nil);
    [self.confirmBtn setTitle:kLocalizedString(@"确定",nil) forState:(UIControlStateNormal)];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (![NSString validateNumber:string]) {
        return NO;
    }
    return YES;
}

#pragma mark - UITextViewDelegate

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@""]) {
        return YES;
    }
    
    if (([NSString isContainsEmoji:text] && ![NSString isNineKeyBoard:text]) || (text.isEmoji && ![NSString isNineKeyBoard:text]) || (([NSString hasEmoji:text] && ![NSString isNineKeyBoard:text]))) {
        return NO;
    }
    
    if ([textView.text length] + [text length] - range.length > 200){
        return NO;
    }
    
    return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
    [self updateRemainCount];
}
- (void)updateRemainCount {
    long count = MAX(0, (int)(200 - self.textView.text.length));
    self.countLabel.text = [NSString stringWithFormat:@"(%ld/200)",200-count];
    self.placeLabel.hidden = ![NSString isBlankString:self.textView.text];
}

#pragma mark - submitAction

- (IBAction)submitAction:(UIButton *)sender {
    
    if ([NSString isBlankString:self.textView.text]) {
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入问题描述",nil)];
        return;
    }else if ([NSString isBlankString:self.phoneTextField.text]){
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入您的手机号",nil)];
        return;
    }else if(![NSString isInputRightPhoneNum:self.phoneTextField.text]){
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确手机号",nil)];
        return;
    }else if ([NSString isBlankString:self.wechatTextField.text]){
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入您的微信号",nil)];
        return;
    }else if (![NSString isWechatName:self.wechatTextField.text]){
        [HFStatusView showFailureMessage:kLocalizedString(@"请输入正确的微信号",nil)];
        return;
    }
    
    [self.view endEditing:YES];
    
    [self startClearBgColorLoading];
    
    @weak(self)
    [UserManager getUserFeedBackWithContent:self.textView.text wxId:self.wechatTextField.text phone:self.phoneTextField.text block:^(id data, HttpRequestResult result, NSString *msg) {
        [self_weak_ stopLoading];
        if (result == kHttpRequestResultNormal) {
            [HFStatusView showSuccessMessage:kLocalizedString(@"提交成功",nil)];
            [self_weak_.navigationController popViewControllerAnimated:YES];
        }else{
            [HFStatusView showFailureMessage:msg];
        }
    }];
}

@end
